package com.saudidentalapp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.saudiapp.lazylist.ImageLoader;
import com.saudidentalapp.datamodels.Exhibitor;
import com.saudidentalapp.datamodels.Poster;
import com.saudidentalapp.datamodels.Program;
import com.saudidentalapp.datamodels.Speaker;

public class SearchAdapter extends ArrayAdapter<Object> implements Filterable{

    private List<Object> allModelItemsArray;
    private List<Object> filteredModelItemsArray;
    private Activity context;
    private ModelFilter filter;
    private LayoutInflater inflator;
    public ImageLoader imageLoader; 

    public SearchAdapter(Activity context, List<Object> list) 
    {
        super(context, R.layout.custom_search_row, list);
        this.context = context;
        this.allModelItemsArray = new ArrayList<Object>();
        allModelItemsArray.addAll(list);
        this.filteredModelItemsArray = new ArrayList<Object>();
        filteredModelItemsArray.addAll(allModelItemsArray);
        inflator = context.getLayoutInflater();
        getFilter();
        imageLoader=new ImageLoader(context.getApplicationContext());
    }
    @Override
    public Filter getFilter() 
    {
        if (filter == null)
        {
          filter  = new ModelFilter();
        }
        return filter;
      }

    static class ViewHolder 
    {
        protected TextView textTitle;
        protected TextView textDescription;    
        protected ImageView imgThumb;     
    }
    
    
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {       
        
        Object m = filteredModelItemsArray.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) 
        {            
        	convertView = inflator.inflate(R.layout.custom_search_row, null);
            viewHolder = new ViewHolder();
            viewHolder.textTitle = (TextView) convertView.findViewById(R.id.xtxtSearchTitle);
            viewHolder.textDescription = (TextView) convertView.findViewById(R.id.xtxtSearchDetails);
            viewHolder.imgThumb = (ImageView) convertView.findViewById(R.id.xtxtSearchImage);
            convertView.setTag(viewHolder);            
        } 
        else 
        {
           
            viewHolder = ((ViewHolder) convertView.getTag());
        }
        
        setSearchData(getItem(position),viewHolder);
       

        return convertView;
    }

     private void setSearchData(Object object, ViewHolder viewHolder) 
     {
    	if(object instanceof Program) 
    	{
    		 Program program=(Program)object;
    		 viewHolder.textTitle.setText(program.getTitle());
        	 viewHolder.textDescription.setText(program.getStartTime() +" - " +program.getEndTime());
        	 viewHolder.imgThumb.setVisibility(View.GONE); 
    	}
    	else if(object instanceof Speaker)
    	{
    		 Speaker speaker=(Speaker)object;
    		 viewHolder.textTitle.setText(speaker.getName());
        	 viewHolder.textDescription.setText(speaker.getLocation());
        	 viewHolder.imgThumb.setVisibility(View.VISIBLE); 
        	 imageLoader.DisplayImage(speaker.getThumbImage(), viewHolder.imgThumb);
    	}
    	else if(object instanceof Exhibitor)
    	{
    		Exhibitor exhibitor=(Exhibitor)object;
    		 viewHolder.textTitle.setText(exhibitor.getName());
        	 viewHolder.textDescription.setText(exhibitor.getDesignation());
        	 viewHolder.imgThumb.setVisibility(View.VISIBLE); 
        	 imageLoader.DisplayImage(exhibitor.getThumbImage(), viewHolder.imgThumb);
    	}
    	else if(object instanceof Poster)
    	{
    		Poster exhibitor=(Poster)object;
	   		 viewHolder.textTitle.setText(exhibitor.getTitle());	       	
	       	 viewHolder.imgThumb.setVisibility(View.VISIBLE); 
	       	 viewHolder.textDescription.setVisibility(View.GONE);
    	}
    	
	}

    String getString(Object object)
    {
    	 String result = null;
        	if(object instanceof Program) 
        	{
        		 Program program=(Program)object;
        		 result=program.getTitle();
            	
        	}
        	else if(object instanceof Speaker)
        	{
        		 Speaker speaker=(Speaker)object;
        		 result=speaker.getName() +" "+speaker.getLocation();            	
        	}
        	else if(object instanceof Exhibitor)
        	{
        		Exhibitor exhibitor=(Exhibitor)object;
        		result=exhibitor.getName() +" "+exhibitor.getDesignation();  
        	}
        	else if(object instanceof Poster)
        	{
        		Poster poster=(Poster)object;
        		result=poster.getTitle();	       	
    	       	 
        	}
        	else
        	{
        		result="";
        	}
			return result;        	
    	}
    
     
     
   
     
     
	private class ModelFilter extends Filter
        {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                
                constraint = constraint.toString().toLowerCase();
                FilterResults result = new FilterResults();
                if(constraint != null && constraint.toString().length() > 0)
                {
                    ArrayList<Object> filteredItems = new ArrayList<Object>();
                   
                    for(int i = 0; i<allModelItemsArray.size(); i++)
                    {
                        String data = (String) getString(allModelItemsArray.get(i));
                        if(data.toLowerCase().contains(constraint))
                            filteredItems.add(allModelItemsArray.get(i));
                    }
                    result.count = filteredItems.size();
                    result.values = filteredItems;
                }
                else
                {
                    synchronized(this)
                    {
                        result.values = allModelItemsArray;
                        result.count = allModelItemsArray.size();
                    }
                }
                return result;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
               
                filteredModelItemsArray = (ArrayList<Object>)results.values;
                notifyDataSetChanged();
                clear();
                for(int i = 0; i< filteredModelItemsArray.size(); i++)
                    add(filteredModelItemsArray.get(i));
                notifyDataSetInvalidated();
                
                if(filteredModelItemsArray.size()>0)
                {
                	dialogListener.onResult(true);
                }
                else
                {
                	dialogListener.onResult(false);
                }
            }
       }
	
	
	void setOnDataListener(DataListener dialogListener)
	{
		this.dialogListener=dialogListener;
	}
	DataListener dialogListener;
	
	
	
	public interface DataListener
	{
		public abstract void onResult(boolean noResult);		
	}
	
	
	
}
